<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml"
      xmlns:th="http://www.thymeleaf.org" xmlns:shiro="shiro.apache.org/tags">
<head>
    <th:block th:include="common/common_head :: commonHeader('报表库新增')"/>
    <link rel="stylesheet" th:href="@{/static/js/plugins/jquery-layout/jquery.layout-latest.css}">
</head>
<body class="gray-bg">
<div class="container-div ui-layout-center">
    <div class="row">
        <div class="form-group">
            <div class="col-sm-8" style="margin-left: 16px">
                <input class="btn btn-primary" type="button" onclick="saveReport()" value="保存">
                <button class="btn btn-primary" type="button" onclick="Dialog.closeFrame(window.name)">取 消</button>
            </div>
        </div>
        <div class="col-sm-12" align="centre" style="margin-top: 10px">
            <form class="ibox-content form-horizontal" method="post" id="reportFrom" enctype="multipart/form-data">
                <div class="form-group row">
                    <label class="col-xs-1 control-label"><span class="required-sign">*</span>检测机构：</label>
                    <div class="col-xs-3">
                        <input class="form-control" name="testingInstitution" type="text" placeholder="检测机构" required>
                    </div>
                    <label class="col-xs-2 control-label"><span class="required-sign">*</span>测试报告编号：</label>
                    <div class="col-xs-3">
                        <input id="reportCode" class="form-control" name="reportCode" type="text" placeholder="测试报告编号" required
                               autocomplete="off">
                    </div>
                </div>
                <div class="form-group row">
                    <label class="col-xs-1 control-label"><span class="required-sign">*</span>附件：</label>
                    <div class="col-xs-3">
                        <input name="attachFile" required type="file" class="form-control">
                    </div>
                    <label class="col-xs-2 control-label"><span class="required-sign">*</span>第三方报告委托单位：</label>
                    <div class="col-xs-3">
                        <input class="form-control" name="consignee" type="text" placeholder="第三方报告委托单位" required>
                    </div>
                </div>
                <div class="form-group row">
                    <label class="col-xs-1 control-label"><span class="required-sign">*</span>报告日期：</label>
                    <div class="col-xs-3">
                        <input type="text" name="reportDate" required id="reportDate" autocomplete="off"
                               class="form-control">
                    </div>
                    <label class="col-xs-2 control-label"><span class="required-sign">*</span>有效日期至：</label>
                    <div class="col-xs-3">
                        <input type="text" name="expireDate" required id="expireDate" autocomplete="off"
                               class="form-control">
                    </div>
                </div>
                <div class="form-group row">
                    <label class="col-xs-1 control-label"><span class="required-sign">*</span>是否替换旧报告：</label>
                    <div class="col-xs-3" style="font-size: 1.3em;">
                        <input name="replaced" type="radio" value="1">是
                        <input name="replaced" type="radio" value="0" checked>否
                    </div>
                    <label class="col-xs-2 control-label">原测试报告编号（替换多个报告用英文逗号,隔开)：</label>
                    <div class="col-xs-3">
                        <input type="text" name="originalReportCode" id="originalReportCode" class="form-control"
                               autocomplete="off">
                    </div>
                </div>
                <div class="form-group row">
                    <label class="col-xs-1 control-label"><span class="required-sign">*</span>提交人：</label>
                    <div class="col-xs-3">
                        <input type="text" name="submitter" class="form-control" placeholder="提交人" autocomplete="off">
                    </div>
                </div>
                <!-- <div class="form-group row">
                     <label class="col-xs-1 control-label">备注：</label>
                     <div class="col-xs-7">
                         <span style="color: red;line-height: 2em;">
                         </span>
                     </div>
                 </div>-->
            </form>
        </div>
    </div>
</div>

<!--引入公共js-->
<div th:include="common/onload_js :: onloadJS"></div>
<script type="text/javascript" th:src="@{/static/js/plugins/jquery-layout/jquery.layout-latest.js}"></script>

<script type="text/javascript">

    $(function () {
        // 初始化时间插件
        laydate.render({elem: '#reportDate'});
        laydate.render({elem: '#expireDate'});
        //初始化所有字典数据
        initDictDataSel();
    });

    //保存提交的数据
    function saveReport() {
        if ($("#reportFrom").valid()) {

            //有效日期不能晚于今天
            var expireDate = new Date($("#expireDate").val());
            if (expireDate < new Date()) {
                Dialog.errorMsg("有效日期不能晚于今天！");
                return;
            }

            //报告日期不能早于今天
            var reportDate = new Date($("#reportDate").val());
            if (reportDate > new Date()) {
                Dialog.errorMsg("报告日期不能早于今天！");
                return;
            }

            //替换旧报告，原测试报告编号必填
            if ($("input[name='replaced']:checked").val() == 1 && !$("#originalReportCode").val().trim()) {
                Dialog.errorMsg("请填写原测试报告编号！");
                return;
            } else if ($("input[name='replaced']:checked").val() == 0) {
                $("#originalReportCode").val('');
            }

            if($("#originalReportCode").val().trim().indexOf($("#reportCode").val().trim())>=0){
                Dialog.errorMsg("测试报告编号不能同原测试报告编号相同！");
                return;
            }

            //验证替换的原测试报告存不存在
            let flag = true;
            if ($("#originalReportCode").val()) {
                $.ajax({
                    url: ctxPath + "/reportRepository/checkReportCode",
                    data: {'reportCode': $("#originalReportCode").val().trim()},
                    dataType: 'JSON',
                    async: false,
                    success: function (data) {
                        layer.closeAll();
                        if (data.result == 0) {
                            flag = false;
                            Dialog.errorMsg("原测试报告编号 " + data.reportCode + " 不存在，请重新输入！",null,{time: 3000});
                        }
                    }
                });
            }

            if (!flag) {
                return;
            }

            let formData = new FormData($('#reportFrom')[0]);
            $.ajax({
                url: ctxPath + "/reportRepository/saveReport",
                type: 'POST',
                data: formData, // 上传formdata封装的数据包
                dataType: 'JSON',
                cache: false, // 不缓存
                processData: false, // jQuery不要去处理发送的数据
                contentType: false, // jQuery不要去设置Content-Type请求头
                async: false,
                success: function (data) {
                    layer.closeAll();
                    if (data.code == 0) {
                        Dialog.msg("操作成功!");
                        parent.refresh();
                        Dialog.closeFrame(window.name);
                    } else {
                        Dialog.errorMsg("操作失败，请重试!");
                    }
                }
            });
        }
    }

</script>

</body>
</html>